/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addOrder.java
 *
 * Created on Oct 31, 2011, 4:06:29 PM
 */

package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
/**
 *
 * @author Ling
 */
public class addOrder extends javax.swing.JFrame {

    /** Creates new form addOrder */
    private F_addstock s = new F_addstock();
    private F_addorder o = new F_addorder();
    private F_addorder o2 = new F_addorder();
    private F_addorderstocks os = new F_addorderstocks();
    private F_addorderstocks os2 = new F_addorderstocks();
    private F_updateorder uo = new F_updateorder();
    private F_updatestock us = new F_updatestock();
    private insufficientstocksFrame in;
    private MainFrame main;
    private int idstock=0;
    private int idproduct=0;
    private int idorder=0;
    private int u_idorder=0;
    boolean update;

    private int o_quantity = 0;
    private int stock_number = 0;
    private int balance = 0;
    private int o_rqty = 0;
    private String sbalance;
    
    private Connection conn;
    private String SQLStatement;
    private String URL;
    private Statement stmt;
    private ResultSet rset;
    protected JTable table;

    public void setTable(JTable table) {
        this.table = table;
    }
    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public addOrder(MainFrame mf, boolean add_update, int orderid) {
        initComponents();
        this.setLocation(400, 150);
        this.setSize(465, 545);
        this.setResizable(false);
        this.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
        main = mf;
        update = add_update;
        u_idorder = orderid;

        // Add items to product combo box
        // including inactive product stocks
        connect();
        try{
            SQLStatement = "SELECT DISTINCT(p_name) from product "
                    + "ORDER BY p_name";
            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()){
                o_productnameCombox.addItem(rset.getString("p_name"));
            }
            rset.close();
            disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }

        // Add items to customer combo box
        connect();
        try{
            SQLStatement = "SELECT DISTINCT(c_name) from customer ORDER BY c_name";
            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()){
                o_customernameCombox.addItem(rset.getString("c_name"));
            }
            rset.close();
            disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }

        if (update){
            uo.connect();
            System.out.println("idorder:"+u_idorder);
            uo.getorder(u_idorder);
            o_productnameCombox.setSelectedItem(uo.getProductinfo("name"));
            o_packingCombox.setSelectedItem(uo.getProductinfo("packing"));
            o_customernameCombox.setSelectedItem(uo.getCustomer());
            o_stocknumCombox.setSelectedItem(Integer.toString(uo.getO_idstock()));
            o_qtysoldField.setText(uo.getO_qtysold());
            o_qtyreturnedField.setText(uo.getO_qtyreturned());
            deliverydateField.setText(uo.getO_datedelivered());
            DRdateField.setText(uo.getO_drdate());
            o_DRnumField.setText(uo.getO_drnum());
            o_WSnumField.setText(uo.getO_wsnum());
            o_WRnumField.setText(uo.getO_wrnum());
            o_notesField.setText(uo.getO_notes());
            uo.disconnect();
        }
    }

    public void setOrder(){
        // order_stock
            idorder = uo.getidorder();
                idstock = Integer.parseInt((String)o_stocknumCombox.getSelectedItem());
                int quantity = Integer.parseInt(o_qtyreturnedField.getText()) + Integer.parseInt(o_qtysoldField.getText());

//                uos.setOs_idstock(idstock);
//                uos.setOs_idorder(idorder);
//
//                // Add entry to order_stock table
//                uos.addorder_stock();

                uo.setO_idorder_stock(os.getidorder_stock()); //int
                uo.setO_idstock(idstock);
                uo.setO_dateposted(date.now());
                uo.setO_datedelivered(deliverydateField.getText());
                uo.setO_drdate(DRdateField.getText());
                uo.setO_drnum(o_DRnumField.getText());
                uo.setO_qtyreturned(o_qtyreturnedField.getText());
                uo.setO_qtysold(o_qtysoldField.getText());
                uo.setO_wrnum(o_WRnumField.getText());
                uo.setO_wsnum(o_WSnumField.getText());
                uo.setO_notes(o_notesField.getText());

                uo.setO_idcustomer(o.getidcustomer(o_customernameCombox.getSelectedItem().toString())); //int

    }

    public void setcombox(String productname){
        o_productnameCombox.setSelectedItem(productname);

        //Add items to packing combobox
        // including inactive product stocks
        o_packingCombox.removeAllItems();
        o_packingCombox.addItem("<Select Packing>");
        connect();
        try{
            SQLStatement = "SELECT p_packing from product "+
                            "WHERE p_name ='"+(String)o_productnameCombox.getSelectedItem()+
                            "' ORDER BY p_packing";
            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()){
                o_packingCombox.addItem(rset.getString("p_packing"));
            }
            rset.close();
            disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        o_WRnumField = new javax.swing.JTextField();
        o_DRnumField = new javax.swing.JTextField();
        o_WSnumField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        o_notesField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        o_productnameCombox = new javax.swing.JComboBox();
        o_packingCombox = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        o_qtysoldField = new javax.swing.JTextField();
        o_customernameCombox = new javax.swing.JComboBox();
        o_viewstockinorderButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        o_stocknumCombox = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        o_qtyreturnedField = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        o_cancelorderButton = new javax.swing.JButton();
        o_saveorderButton = new javax.swing.JButton();
        o_searchproductnameButton = new javax.swing.JButton();
        deliverydateField = new datechooser.beans.DateChooserCombo();
        DRdateField = new datechooser.beans.DateChooserCombo();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Manage Order");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        o_WRnumField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_WRnumFieldActionPerformed(evt);
            }
        });
        getContentPane().add(o_WRnumField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 330, 140, -1));

        o_DRnumField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_DRnumFieldActionPerformed(evt);
            }
        });
        getContentPane().add(o_DRnumField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 270, 140, -1));

        o_WSnumField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_WSnumFieldActionPerformed(evt);
            }
        });
        getContentPane().add(o_WSnumField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 300, 140, -1));

        jLabel6.setText("Product Name");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 130, 20));

        o_notesField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_notesFieldActionPerformed(evt);
            }
        });
        getContentPane().add(o_notesField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 360, 260, 60));

        jLabel7.setText("Packing");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, 120, 20));

        o_productnameCombox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Select Product>" }));
        o_productnameCombox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_productnameComboxActionPerformed(evt);
            }
        });
        getContentPane().add(o_productnameCombox, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 30, 230, 20));

        o_packingCombox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Select Packing>" }));
        o_packingCombox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_packingComboxActionPerformed(evt);
            }
        });
        getContentPane().add(o_packingCombox, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 60, 140, -1));

        jLabel8.setText("Quantity Sold");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 150, 80, 20));

        jLabel9.setText("Customer Name");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, -1, 20));

        o_qtysoldField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_qtysoldFieldActionPerformed(evt);
            }
        });
        getContentPane().add(o_qtysoldField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 150, 40, -1));

        o_customernameCombox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Select Customer>" }));
        getContentPane().add(o_customernameCombox, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 90, 140, -1));

        o_viewstockinorderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/shopping-basket-info-icon.png"))); // NOI18N
        o_viewstockinorderButton.setText("View Stock");
        o_viewstockinorderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_viewstockinorderButtonActionPerformed(evt);
            }
        });
        getContentPane().add(o_viewstockinorderButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 120, 110, -1));

        jLabel10.setText("Warehouse Releasing No.");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 150, 20));

        jLabel11.setText("Delivery Date");
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 210, -1, 20));

        jLabel12.setText("Delivery Receipt Date");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 240, -1, 20));

        jLabel13.setText("Withdrawal Slip No.");
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 300, -1, 20));

        jLabel14.setText("Delivery Receipt No.");
        getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 270, -1, 20));

        jLabel15.setText("Stock Number");
        getContentPane().add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 110, 20));

        o_stocknumCombox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Select Stock No.>" }));
        getContentPane().add(o_stocknumCombox, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 120, 140, -1));

        jLabel16.setText("Quantity Returned");
        getContentPane().add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, -1, 20));

        jLabel17.setText("Notes");
        getContentPane().add(jLabel17, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 360, 150, 20));
        getContentPane().add(o_qtyreturnedField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 180, 40, -1));
        getContentPane().add(jTextField8, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 180, 40, -1));

        o_cancelorderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Button-Close-icon.png"))); // NOI18N
        o_cancelorderButton.setText("Cancel");
        o_cancelorderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_cancelorderButtonActionPerformed(evt);
            }
        });
        getContentPane().add(o_cancelorderButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 470, 100, -1));

        o_saveorderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Save-icon.png"))); // NOI18N
        o_saveorderButton.setText("Save");
        o_saveorderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_saveorderButtonActionPerformed(evt);
            }
        });
        getContentPane().add(o_saveorderButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 440, 100, -1));

        o_searchproductnameButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Zoom-icon (1).png"))); // NOI18N
        o_searchproductnameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                o_searchproductnameButtonActionPerformed(evt);
            }
        });
        getContentPane().add(o_searchproductnameButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 30, 30, 20));
        getContentPane().add(deliverydateField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 210, 140, -1));
        getContentPane().add(DRdateField, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 240, 140, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean checkOrder(int qtyordered, int s_number)
    {
            boolean enough= true;

            o_quantity = qtyordered;
            stock_number = s_number;
            balance = 0;

            connect();
            SQLStatement = "SELECT idstock, s_balance FROM stock WHERE idstock = " + stock_number;
            try {
            rset = stmt.executeQuery(SQLStatement);
            if(rset.next()){
                stock_number = rset.getInt("idstock");
                sbalance = rset.getString("s_balance");
            }
            System.out.println("balace of stock: "+sbalance+" stock num: "+stock_number+" qtyordered: "+o_quantity);
            if(Integer.parseInt(sbalance) < o_quantity)
               enough = false;

            System.out.println("orig stock: "+stock_number+" balance: "+sbalance );
            rset.close();
            disconnect();
            }
            catch(Exception ex){
                System.out.println(ex.getMessage());
                JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
            }
            return enough;
    }

    private boolean checkValidInputs()
    {
        Boolean valid = false;
        warningFrame wa;

        int qty = 0, qty_returned = 0;
        String notes, wrn, wsn,drn;
        int wrn_n,wsn_n,drn_n;

        notes = o_notesField.getText();
        wrn = o_WRnumField.getText();
        wsn = o_WSnumField.getText();
        drn = o_DRnumField.getText();

        if((notes.length()<200)&&(drn.length()==4)&&(wsn.length()==6)&&(wrn.length()==5))
        {
            try {
                drn_n = Integer.parseInt(drn);
                wsn_n = Integer.parseInt(wsn);
                wrn_n = Integer.parseInt(wrn);

                qty = Integer.parseInt(o_qtysoldField.getText());
                qty_returned = Integer.parseInt(o_qtyreturnedField.getText());
                if((qty>=0)&&(qty_returned>=0))
                {
                valid = true;
                }
            }
            catch(NumberFormatException e)
            {
                wa = new warningFrame("invaliddata", this);
                wa.setVisible(true);
            }
        }
         else {
            wa = new warningFrame("invaliddata", this);
            wa.setVisible(true);
         }
    return valid;

}
    private void o_WRnumFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_WRnumFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_o_WRnumFieldActionPerformed

    private void o_DRnumFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_DRnumFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_o_DRnumFieldActionPerformed

    private void o_WSnumFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_WSnumFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_o_WSnumFieldActionPerformed

    private void o_notesFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_notesFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_o_notesFieldActionPerformed

    private void o_qtysoldFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_qtysoldFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_o_qtysoldFieldActionPerformed

    private void o_viewstockinorderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_viewstockinorderButtonActionPerformed
        // TODO add your handling code here:
        if((String)o_stocknumCombox.getSelectedItem()!="<Select Stock No.>"){
            this.setEnabled(false);
            idstock = Integer.parseInt((String)o_stocknumCombox.getSelectedItem());
            idproduct = s.getidproduct((String)o_productnameCombox.getSelectedItem(), (String)o_packingCombox.getSelectedItem());

            viewStock_in_addOrder viewstock = new viewStock_in_addOrder(this, idstock, idproduct);
            System.out.println(idproduct+" "+idstock);
            viewstock.setVisible(true);
        }
}//GEN-LAST:event_o_viewstockinorderButtonActionPerformed

    private void o_saveorderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_saveorderButtonActionPerformed
        // TODO add your handling code here:
//        main.setEnabled(true);
//        this.setVisible(false);
//        this.dispose();
        int lackingqty=0;
        int newidstock=0;

        int quantity;
        boolean enough=true;
        confirmFrame co;
        warningFrame wa;
        this.setEnabled(false);

        if((o_qtysoldField.getText().equals("")) || (o_qtyreturnedField.getText().equals("")) ||
                (o_DRnumField.getText().equals("")) || (o_WRnumField.getText().equals("")) ||
                (o_WSnumField.getText().equals("")) || (o_stocknumCombox.getSelectedItem()=="<Select Stock>") ||
                (deliverydateField.getText().equals("")) || (DRdateField.getText().equals("")) ||
                (o_stocknumCombox.getSelectedItem().toString().equals("<Select Stock No.>")) ||
                (o_customernameCombox.getSelectedItem().toString().equals("<Select Customer>")) ||
                (o_productnameCombox.getSelectedItem().toString().equals("<Select Product>")) ||
                (o_packingCombox.getSelectedItem().toString().equals("<Select Packing>")))
        {
             wa = new warningFrame("invaliddata", this);
             wa.setVisible(true);
        }

        else
        {
            if (checkValidInputs()){
                idorder = o.getidorder();
                idstock = Integer.parseInt((String)o_stocknumCombox.getSelectedItem());
                quantity = Integer.parseInt(o_qtyreturnedField.getText()) + Integer.parseInt(o_qtysoldField.getText());
                System.out.println("IDSTOCK"+idstock);
                os.setOs_idstock(idstock);
                os.setOs_idorder(idorder);

                // Add entry to order_stock table
                os.addorder_stock();

                o.setO_idorder_stock(os.getidorder_stock()); //int
                o.setO_idstock(idstock);
                o.setO_dateposted(date.now());
                o.setO_datedelivered(deliverydateField.getText());
                o.setO_drdate(DRdateField.getText());
                o.setO_drnum(o_DRnumField.getText());
                o.setO_qtyreturned(o_qtyreturnedField.getText());
                o.setO_qtysold(o_qtysoldField.getText());
                o.setO_wrnum(o_WRnumField.getText());
                o.setO_wsnum(o_WSnumField.getText());
                o.setO_notes(o_notesField.getText());

                o.setO_idcustomer(o.getidcustomer(o_customernameCombox.getSelectedItem().toString())); //int

                System.out.println("quantity "+quantity);
                enough = checkOrder (quantity ,idstock);
                if(enough)
                {
                    if (update){
                        setOrder();
                        System.out.println("setOrder idstock:"+idstock);
                        co = new confirmFrame(main, this, uo, "order", update);
                    }
                    else
                    co = new confirmFrame(main, this, o, "order", update);

                    co.setTable(table);
                    co.setVisible(true);
                }
                else { // when stock cannot accomodate order quantity
                    // stock_number balance o_quantity o_rqty os_idorder_stock

                    // when stock cannot accomodate order quantity
                        // stock_number balance o_quantity o_rqty os_idorder_stock

                        o.setO_qtysold(sbalance);
                        o_rqty = o_quantity - Integer.parseInt(sbalance);

                        System.out.println("qty:"+sbalance+" stock:"+idstock+" order:"+idorder+" os:"+os.getidorder_stock());
                        System.out.println("o_rqty"+o_rqty);
                        int iddstock = idstock;
                        connect();
                        try {
                            SQLStatement = "SELECT * FROM stock, product WHERE s_balance >= " + o_rqty
                                    + " AND product.p_packing = '" + o_packingCombox.getSelectedItem().toString() + "' "
                                    + "AND product.p_name = '" + o_productnameCombox.getSelectedItem().toString() + "' "
                                    + "AND product.idproduct = stock.s_idproduct "
                                    + "AND idstock!="+iddstock+"";
                            rset = stmt.executeQuery(SQLStatement);
                            if(rset.next()){
                                stock_number = rset.getInt("idstock");
                            }
                            rset.close();
                            disconnect();
                            System.out.println("second stock: "+stock_number);
                        } catch (SQLException ex) {
                        Logger.getLogger(addOrder.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        if (stock_number != iddstock){
                            in = new insufficientstocksFrame(main, this, o, table, Integer.toString(stock_number), Integer.toString(o_rqty));
                            in.setTable(table);
                            in.setOss(os);
                            in.setVisible(true);
                        }
                         else{
                            System.out.println("DITO BA");
                            wa = new warningFrame("nostocks", this);
                            wa.setVisible(true);
                         }
                }
            }
             else {
                wa = new warningFrame("invaliddata", this);
                wa.setVisible(true);
             }

        }

        //co = new confirmFrame(main, this, o, "order");
        //co.setVisible(true);
//        warningFrame wa = new warningFrame("lackofstocks", this);
//        wa.setVisible(true);
//        warningFrame wa2 = new warningFrame("invaliddata", this);
//        wa2.setVisible(true);
    }//GEN-LAST:event_o_saveorderButtonActionPerformed

    private void o_cancelorderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_cancelorderButtonActionPerformed
        // TODO add your handling code here:
        main.setEnabled(true);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_o_cancelorderButtonActionPerformed

    private void o_searchproductnameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_searchproductnameButtonActionPerformed
        // TODO add your handling code here:
        searchproductname_in_addstockorder sp = new searchproductname_in_addstockorder(this, "order");
        sp.setVisible(true);
        this.setEnabled(false);
    }//GEN-LAST:event_o_searchproductnameButtonActionPerformed

    private void o_productnameComboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_productnameComboxActionPerformed
        // TODO add your handling code here:

        //Add items to packing combobox
        // including inactive product stocks
        o_packingCombox.removeAllItems();
        o_packingCombox.addItem("<Select Packing>");
        connect();
        try{
            SQLStatement = "SELECT p_packing from product "+
                            "WHERE p_name ='"+(String)o_productnameCombox.getSelectedItem()+
                            "' ORDER BY p_packing";
            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()){
                o_packingCombox.addItem(rset.getString("p_packing"));
            }
            rset.close();
            disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_o_productnameComboxActionPerformed

    private void o_packingComboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_o_packingComboxActionPerformed
        // TODO add your handling code here:
        idproduct = s.getidproduct((String)o_productnameCombox.getSelectedItem(), (String)o_packingCombox.getSelectedItem());

        // Add items to stock no. combo box
        // include ONLY stocks w balance>=1
        o_stocknumCombox.removeAllItems();
        o_stocknumCombox.addItem("<Select Stock No.>");
        connect();
        try{
            SQLStatement = "SELECT idstock FROM stock "
                    + "WHERE s_balance >= 1 AND s_idproduct = "+ idproduct;
            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()){
                o_stocknumCombox.addItem(rset.getString("idstock"));
            }
            rset.close();
            disconnect();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_o_packingComboxActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private datechooser.beans.DateChooserCombo DRdateField;
    private datechooser.beans.DateChooserCombo deliverydateField;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField o_DRnumField;
    private javax.swing.JTextField o_WRnumField;
    private javax.swing.JTextField o_WSnumField;
    private javax.swing.JButton o_cancelorderButton;
    private javax.swing.JComboBox o_customernameCombox;
    private javax.swing.JTextField o_notesField;
    private javax.swing.JComboBox o_packingCombox;
    private javax.swing.JComboBox o_productnameCombox;
    private javax.swing.JTextField o_qtyreturnedField;
    private javax.swing.JTextField o_qtysoldField;
    private javax.swing.JButton o_saveorderButton;
    private javax.swing.JButton o_searchproductnameButton;
    private javax.swing.JComboBox o_stocknumCombox;
    private javax.swing.JButton o_viewstockinorderButton;
    // End of variables declaration//GEN-END:variables

}
